package com.lanou.beautifulwardrobe.Brand;


import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.Response;
import com.lanou.beautifulwardrobe.R;
import com.lanou.beautifulwardrobe.activity.ProductDetailActivity;
import com.lanou.beautifulwardrobe.contants.VolleySingleton;
import com.lanou.beautifulwardrobe.entify.EditRecommendProduct;

import java.util.List;

/**
 * Created by dllo on 15/10/31.
 */
public class EditFragment extends Fragment implements View.OnClickListener {
    private EditRecommendProduct datas;
    private int position;
    private TextView priceTv, editerTv, volumeTv, descriptionTv, numTv, intoshopTv;
    private ImageView productIv, editorImage, flagIv;
    private ImageButton ExitBtn;
    private VolleySingleton volleySingleton = VolleySingleton.getInstance();
    private String urlId;

    public EditFragment(List<EditRecommendProduct> products, int position) {
        this.position = position;
        this.datas = products.get(position);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

//        FragmentTransaction ft2 = getFragmentManager().beginTransaction();
//        ft2.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
//        ft2.addToBackStack(null);
//        ft2.commit();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.brandedit, null);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        ExitBtn = (ImageButton) view.findViewById(R.id.ExitBtn);
        priceTv = (TextView) view.findViewById(R.id.priceTv);
        editerTv = (TextView) view.findViewById(R.id.editerTv);
        volumeTv = (TextView) view.findViewById(R.id.volumeTv);
        descriptionTv = (TextView) view.findViewById(R.id.descriptionTv);
        intoshopTv = (TextView) view.findViewById(R.id.intoshopTv);
        numTv = (TextView) view.findViewById(R.id.numTv);

        productIv = (ImageView) view.findViewById(R.id.productIv);
        editorImage = (ImageView) view.findViewById(R.id.editorImage);
        flagIv = (ImageView) view.findViewById(R.id.flagIv);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        initData();
    }

    private void initData() {
        if (datas != null) {

            editerTv.setText(datas.getEditor_name());
            volumeTv.setText(datas.getTaobao_volume() + "");
            descriptionTv.setText(datas.getDescription());
            numTv.setText((position + 1) + "");
            getBitmap(datas.getTaobao_pic_url(), productIv);
            getBitmap(datas.getEditor_logo_url(), editorImage);
            this.urlId=datas.getTaobao_num_iid();

        }
        ExitBtn.setOnClickListener(this);
        productIv.setOnClickListener(this);
    }

    private void getBitmap(String url, final ImageView imageView) {
        volleySingleton.getImageResuest(url, new Response.Listener<Bitmap>() {
            @Override
            public void onResponse(Bitmap response) {
                imageView.setImageBitmap(response);
            }
        });
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.ExitBtn:
                DialogFragment df = (DialogFragment) getActivity().getSupportFragmentManager().findFragmentByTag("dialogFragment");
                df.dismiss();
                break;
            case  R.id.productIv:
                Intent intent =new Intent(getActivity(), ProductDetailActivity.class);
                intent.putExtra("id", urlId);
                getActivity().startActivity(intent);
                break;
        }
    }
}

